package com.shunhe.storage.base;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.router_module.util.AlertUtil;
import com.shunhe.storage.R;
import com.shunhe.storage.dao.model.Goods;
import com.shunhe.storage.dao.model.RepoStaff;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseReceiveOrderActivity extends BaseActivity {
    protected RecyclerView rcv_list;// 出入库货品列表
    protected TextView tv_sel_staff;// 选择工作人员
    protected AppCompatSpinner sp_sel;// 下拉选人员

    protected ReceiveOrderOutDetailAdapter adapter;
    protected List<Goods> goodsList = new ArrayList<>();

    protected RepoStaffListAdapter staffAdapter;
    protected List<RepoStaff> staffList = new ArrayList<>();
    protected RepoStaff currStaff;// 记录当前选择的出入库人

    protected View alert_view;
    protected AlertDialog alert;// 弹窗

    public class RepoStaffListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return staffList.size();
        }

        @Override
        public Object getItem(int i) {
            return staffList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v = View.inflate(BaseReceiveOrderActivity.this, R.layout.sp_item_sel_staff_list, null);
            TextView tv_staff = v.findViewById(R.id.tv_staff);

            RepoStaff staff = staffList.get(i);
            tv_staff.setText(staff.getName());
            return v;
        }
    }

    // 货品列表
    public class ReceiveOrderOutDetailAdapter extends ListBaseAdapter<Goods> {

        public ReceiveOrderOutDetailAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_receive_order_out_detail_goods_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            RecyclerView rcv_storage_bin = holder.getView(R.id.rcv_storage_bin);
            TextView tv_distribute = holder.getView(R.id.tv_distribute);

            Goods goods = mDataList.get(position);

            LinearLayoutManager manager = new LinearLayoutManager(BaseReceiveOrderActivity.this);
            StorageBinListAdapter adapter = new StorageBinListAdapter(BaseReceiveOrderActivity.this);
            rcv_storage_bin.setLayoutManager(manager);
            rcv_storage_bin.setAdapter(adapter);
            List<Goods.StorageBin> binList = goods.getBinList();
            adapter.setDataList(binList);

            // 分配仓库
            tv_distribute.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ToastUtils.showShort("分配仓库");
                    showInRepoDialog();
                }
            });
        }
    }

    // 货品列表里面的库位列表
    public class StorageBinListAdapter extends ListBaseAdapter<Goods.StorageBin> {

        public StorageBinListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_storage_bin_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            TextView tv_sto_bin_name = holder.getView(R.id.tv_sto_bin_name);
            TextView tv_repo_out_num = holder.getView(R.id.tv_repo_out_num);
        }
    }

    public void showInRepoDialog() {// 显示入库弹窗
        alert.show();
        alert_view = View.inflate(this, R.layout.alert_in_repo_layout, null);
        sp_sel = alert_view.findViewById(R.id.sp_sel);
        sp_sel.setAdapter(staffAdapter);
        sp_sel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                currStaff = staffList.get(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        alert_view.findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_sel_staff.setText(currStaff.getName());
                alert.dismiss();
            }
        });

        AlertUtil.setAlertCenterStyle(alert.getWindow(), alert_view, this);
    }
}